﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Microsoft.VisualStudio.TestTools.UnitTesting;

using Pibby.Core;


namespace Pibby.Core.Tests
{
	[TestClass]
	public class EpisodeTests
	{
		[TestMethod]
		public void InitialEpisodeConditions()
		{
			Episode ep = new Episode();
			//Assert.IsNotEmpty(ep.Id);

			//Assert.IsFalse(ep.IsSelected);
			Assert.IsNull(ep.PodcastName);
			Assert.IsNull(ep.Name);
			Assert.IsNull(ep.Feed);
		}

		[TestMethod]
		[ExpectedException(typeof(ArgumentException))]
		public void CannotChangeEpisodeIdFirstTime()
		{
			// for persistence purposes
			Episode ep = new Episode();
			//Assert.IsNotEmpty(ep.Id);

			ep.Id = "changedid";
		}

		[TestMethod]
		public void CanSetIdAtConstruction()
		{
			const string id = "123456789";
			Episode ep = new Episode("My Podcast", new Uri("http://example.com/"), id);
			Assert.AreEqual(id, ep.Id);
		}


		[TestMethod]
		public void EpisodesAreEqualInConstruction()
		{
			string name = "Test";
			Uri link = new Uri("http://example.com");

			Episode ep1 = new Episode();
			ep1.Name = name;
			ep1.Feed = link;

			Episode ep2 = new Episode(name, link);

			Assert.AreEqual(ep1.Name, ep2.Name);
			Assert.AreEqual(ep1.Feed, ep2.Feed);
			Assert.AreNotEqual(ep1.Id, ep2.Id);
		}

		[TestMethod]
		public void EpisodeFeedCanBeChanged()
		{
			Episode ep = new Episode();

			string link1 = "http://example.com/episode1.mp3";
			string link2 = "http://other.example.com/episode2.mp3";

			ep.Feed = new Uri(link1);

			Assert.AreEqual(link1, ep.Feed.AbsoluteUri);
			Assert.AreEqual("episode1.mp3", ep.FileName);

			ep.Feed = new Uri(link2);

			Assert.AreEqual(link2, ep.Feed.AbsoluteUri);
			Assert.AreEqual("episode2.mp3", ep.FileName);
		}

		[TestMethod]
		public void EpisodeNameCanBeChanged()
		{
			Episode ep = new Episode();

			string link = "http://other.example.com/episode.mp3";
			string name1 = "Episode 1";
			string name2 = "Episode 2";

			ep.Feed = new Uri(link);
			ep.Name = name1;

			Assert.AreEqual(name1 + " : " + link, ep.ToString());

			ep.Name = name2;

			Assert.AreEqual(name2 + " : " + link, ep.ToString());
		}

		[TestMethod]
		public void EpisodeFilenameCanBeUsedInFileSystem()
		{
			Episode ep = new Episode("Episode 1", new Uri("http://phpstyle.com/image.jpg?querystring"));

			Assert.AreEqual("image.jpg?querystring", ep.FileName);
			Assert.AreEqual("image.jpg", ep.SafeFileName);
		}
	}
}
